//
//  FeedbackViewController.m
//  myhuibao
//
//  Created by li jun on 13-4-3.
//  Copyright (c) 2013年 myhuibao. All rights reserved.
//
#import<QuartzCore/QuartzCore.h>
#import "FeedbackViewController.h"
#import "ASIFormDataRequest.h"
#import "SBJson.h"
#import "OMGToast.h"
@interface FeedbackViewController ()

@end

@implementation FeedbackViewController
@synthesize tvFeedback;
@synthesize tfMobile;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)addCloseBar{
    UIToolbar * topView = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
    [topView setBarStyle:UIBarStyleDefault];
    
    UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    UIBarButtonItem * doneButton = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(dismissKeyBoard)];
    NSArray * buttonsArray = [NSArray arrayWithObjects:btnSpace, doneButton, nil];
    
    [topView setItems:buttonsArray];
    [tvFeedback setInputAccessoryView:topView];
}
-(IBAction)dismissKeyBoard
{
    [tvFeedback resignFirstResponder];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self addCloseBar];
    tfMobile.delegate = self;
    tvFeedback.layer.cornerRadius=5;
    tvFeedback.layer.borderWidth=1;
    tvFeedback.layer.borderColor=[UIColor lightGrayColor].CGColor;
    [[tvFeedback layer] setShadowOffset:CGSizeMake(1, 1)];
    [[tvFeedback layer] setShadowRadius:3];
    [[tvFeedback layer] setShadowOpacity:1];
    [[tvFeedback layer] setShadowColor:[UIColor lightGrayColor].CGColor];

}

- (void)viewDidUnload
{
    [self setTvFeedback:nil];
    [self setTfMobile:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
    [theTextField resignFirstResponder];
    return YES;
}
- (IBAction)endEdit:(id)sender {
    NSTimeInterval animationDuration = 0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    CGRect rect = CGRectMake(0.0f, 0.0f, self.view.frame.size.width, self.view.frame.size.height);
    self.view.frame = rect;
    [UIView commitAnimations];
    [tfMobile resignFirstResponder];
}

- (IBAction)starEdit:(id)sender {
    float height = 100;
    CGRect frame = self.view.frame;
    frame.origin.y=-height;
    [UIView beginAnimations:@"Curl"context:nil];//动画开始
    [UIView setAnimationDuration:0.30];
    [UIView setAnimationDelegate:self];
    [self.view setFrame:frame];
    [UIView commitAnimations];
}

- (IBAction)doSubmit:(id)sender {
    if(tvFeedback.text.length==0){
        UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示"
                                                      message: @"请输入反馈内容"                                              delegate:nil
                                            cancelButtonTitle:@"确定"
                                            otherButtonTitles:nil];
        [alert show];
        return;
    }
    if(tfMobile.text.length==0){
        UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示"
                                                      message: @"请输入姓名/邮箱/手机号码"                                              delegate:nil
                                            cancelButtonTitle:@"确定"
                                            otherButtonTitles:nil];
        [alert show];
        return;
    }
    //异步请求
    NSURL *url = [NSURL URLWithString:url_feedback];
    ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url];
    [request setPostValue:tvFeedback.text forKey:@"feedback.feedback"];
    [request setPostValue:tfMobile.text forKey:@"feedback.contact"];
    [request setShouldAttemptPersistentConnection:NO];
    [request setDelegate: self];
    [request startAsynchronous];

}

- (IBAction)doBack:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
//下面是两个代理函数,当数据请求成功时会调用requestFinished，请求失败时（如网络问题或服务器内部错误）会调用requestFailed
- (void)requestFinished:(ASIHTTPRequest *)request
{
    // Use when fetching text data
    NSString *jsonStr = [request responseString];
    NSDictionary *dic = [jsonStr JSONValue];
    NSString * message = @"";
    if([[dic objectForKey:@"status"]  isEqualToString:@"SUCCESS"]){
        message = @"提交成功，祝你生活愉快。";
    }else{
        message = [dic valueForKey:@"log"];
    }
    UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示"
                                                  message: message                                              delegate:nil
                                        cancelButtonTitle:@"确定"
                                        otherButtonTitles:nil];
    [alert show];
}
- (void)requestFailed:(ASIHTTPRequest *)request
{
    NSError *error = [request error];
    NSLog(@"%@",error);
    if (error.code==net_failure_code) {
        [OMGToast showWithText:net_failure_msg bottomOffset:100];
    }
}
@end
